import { adjust_val, obj_str, loc, uJob, reload_col, d_sort, cline } from 'sui';

const reload = (hDone, cur, setMain) => {
    uJob(hDone, "away/load",
        {
            line: cline(),
            tech: obj_str(cur, "sn"),
        },
        r => {
            d_sort(r, "sn");

            r.forEach((d, i) => {
                adjust_val(d);
                d["index"] = i + 1;
            });

            setMain(r);
        }, e => setMain(null));
};

const fix_psn = () => {
    return [
        {sn: "boxaway", name: loc("l_800"), tp: "string"},
        {sn: "tech", name: loc("l_363"), tp: "string"},
    ];
};

const load_col = (setCol) => {
    const fix = fix_psn();
    const set = new Set();

    const ignore = ["boxin", "boxout", "boxpair", "boxpack"];
    ignore.forEach(d => set.add(d));

    reload_col(null, v => {
        const tmp = v.filter(d => {
            if (set.has(d.sn)) {
                return false;
            }

            return true;
        });

        setCol(tmp);
    }, "psn", fix);
};

export {load_col, reload};
